import os 
import sys
import socket
import subprocess

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *




class MainWin(QWidget):

    def __init__(self):
        super(MainWin,self).__init__()
        self.setWindowTitle('共享目录')
        UE_COLOR_STYLES = "background-color:#2d2e36;color:rgb(200,200,200);font-size:18px;font-family:KaiTi"
        BTN_STYLE_UE_PADDING5 = '''QPushButton{color:#C8C8C8}
                                QPushButton:hover{color:#FF7F24}
                                QPushButton{background-color:#384551}
                                QPushButton{border:2px}
                                QPushButton{border-radius:3px}
                                QPushButton{padding:5px 5px}'''

        self.setStyleSheet(UE_COLOR_STYLES)
        self.setFixedSize(QSize(350, 130))        
        self.main_layout = QVBoxLayout()
        self.content_label = QLabel('该操作会在当前位置创建CGAI唯一共享目\n是否创建?')
        self.content_label.setAlignment(Qt.AlignCenter)
        self.btn_layout = QHBoxLayout()
        self.ok_btn = QPushButton('是')
        self.ok_btn.setStyleSheet(BTN_STYLE_UE_PADDING5)
        self.ok_btn.clicked.connect(self._ok)
        self.no_btn = QPushButton('否')
        self.no_btn.clicked.connect(lambda:self.close())
        self.no_btn.setStyleSheet(BTN_STYLE_UE_PADDING5)
        self.btn_layout.addWidget(self.ok_btn)
        self.btn_layout.addWidget(self.no_btn)
        self.main_layout.addWidget(self.content_label)
        self.main_layout.addLayout(self.btn_layout)
        self.setLayout(self.main_layout)




    def get_ip(self):
        s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        s.connect(("8.8.8.8",80))
        a = s.getsockname()
            
        return a[0]

    def share_cgai(self):
        # ROOT = os.path.dirname(__file__)
        ROOT = os.path.dirname(sys.executable) 
        
        if 'CGAI' in ROOT:
            QMessageBox.information(None,'提示','该路径前已包含CGAI,请切换路径')
            return

        CGAI = os.path.join(ROOT,'CGAI').replace('/','\\')
        os.makedirs(os.path.join(ROOT,'CGAI'),exist_ok=True)
        
        # 删除cgai共享
        del_cgai_share = "net share cgai /delete /y"

        # 创建cgai共享
        create_cgai_share = f"net share cgai={CGAI} /grant:everyone,FUll"
        to_everyone = f"cacls {CGAI} /t /e /c /g everyone:F"

        c1 = subprocess.Popen(del_cgai_share,shell=True)
        c1.wait()
        print('完成1')
        c2 = subprocess.Popen(create_cgai_share,shell=True)
        c2.wait()
        print('完成2')
        c3 = subprocess.Popen(to_everyone,shell=True)
        c3.wait()
        print('完成3')

        ip = self.get_ip()
        net_path = f"//{ip}/cgai"

        return net_path

    def _ok(self):

        # try:
        path = self.share_cgai()
        print('path:',path)
        if os.path.exists(path):
            QMessageBox.information(None,'成功','设置成功')
            
        else:
            QMessageBox.information(None,'失败','设置失败')
        self.close()
        
if __name__ == '__main__':

        app = QApplication(sys.argv)
        t = MainWin()
        t.show()
        sys.exit(app.exec_())





